package com.xyy.saas.payment.merchant.core.dto;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.cores.enums.ResultCodeEnum;
import com.xyy.saas.payment.util.AmountCastUtil;
import lombok.Builder;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

@Data
@Builder
public class SettleCancelDto {
    /**
     * 业务线
     */
    private String businessIdType;
    /**
     * 支付平台商户标识
     */
    private String accountId;
    /**
     * 分账出金子账户
     */
    private String origOutAccountNo;
    /**
     * 原支付子订单号
     */
    private String origOrderNo;
    /**
     * 原支付订单所属日期
     */
    private Date origOrderDate;
    /**
     * 退款订单号
     */
    private String refundOrderNo;
    /**
     * 逆向交易号
     */
    private String refundTransferNo;
    /**
     * 总分账撤销金额
     */
    private Integer totalAmount;
    /**
     * 分润详情
     */
    private List<SettleCancelItem> orderItems;
    /**
     * 备注
     */
    private String remark;

    /**
     * 组装参数
     */
    public JSONArray assembleOrderItems() {
        if(CollectionUtils.isEmpty(orderItems)) {
            return null;
        }

        JSONArray result = new JSONArray();

        for(SettleCancelItem item : orderItems) {
            JSONObject obj = new JSONObject();
            obj.put("OldSubOrderNo", item.getOrigOrderNo());
            obj.put("OldInSubAcctNo", item.getOrigInAccountNo());
            obj.put("SubAcctCancelAmt", AmountCastUtil.castFen2Yuan(item.getAmount()));
            obj.put("SubAcctCancelRemark", item.getRemark());
            result.add(obj);
        }

        return result;
    }

    /**
     * 参数校验
     */
    public void checkParam() throws PaymentException {
        if(StringUtils.isBlank(businessIdType)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "业务线不能为空");
        }

        if(StringUtils.isBlank(accountId)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "商户标识不能为空");
        }

        if(StringUtils.isBlank(origOrderNo)) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "原支付单号不能为空");
        }

        if(origOrderDate == null) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "原支付日期不能为空");
        }

        if(totalAmount == null || totalAmount <= 0) {
            throw new PaymentException(ResultCodeEnum.PARAM_ERROR.getCode(), "撤销金额不能为空");
        }
    }

    /**
     * 构建详情
     */
    public void buildDetail(String origOrderNo, String origInAccountNo, Integer amount, String remark) {
        setOrderItems(Lists.newArrayList(SettleCancelItem.builder().
                origOrderNo(origOrderNo).
                origInAccountNo(origInAccountNo).
                amount(amount).
                remark(remark).
                build()));
    }
}

@Data
@Builder
class SettleCancelItem {
    /**
     * 分账子单号
     */
    private String origOrderNo;
    /**
     * 分账入金子账户
     */
    private String origInAccountNo;
    /**
     * 分账撤销金额
     */
    private Integer amount;
    /**
     * 分账备注
     */
    private String remark;
}
